using System;
using System.Diagnostics.CodeAnalysis;
using EasyNetQ;
using MalwareMultiScan.Backends.Backends.Implementations;
using MalwareMultiScan.Backends.Enums;
using MalwareMultiScan.Backends.Interfaces;
using MalwareMultiScan.Backends.Services.Implementations;
using MalwareMultiScan.Backends.Services.Interfaces;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace MalwareMultiScan.Backends.Extensions
{
    /// <summary>
    /// Extensions for IServiceCollection.
    /// </summary>
    [ExcludeFromCodeCoverage]
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// Add RabbitMQ service.
        /// </summary>
        /// <param name="services">Service collection.</param>
        /// <param name="configuration">Configuration.</param>
        public static void AddRabbitMq(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton(x =>
                RabbitHutch.CreateBus(configuration.GetConnectionString("RabbitMQ")));
        }

        /// <summary>
        /// Add scanning backend.
        /// </summary>
        /// <param name="services">Service collection.</param>
        /// <param name="configuration">Configuration.</param>
        /// <exception cref="ArgumentOutOfRangeException">Unknown backend.</exception>
        public static void AddScanningBackend(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton<IProcessRunner, ProcessRunner>();

            switch (configuration.GetValue<BackendType>("BackendType"))
            {
                case BackendType.Dummy:
                    services.AddSingleton<IScanBackend, DummyScanBackend>();
                    break;
                case BackendType.Defender:
                    services.AddSingleton<IScanBackend, WindowsDefenderScanBackend>();
                    break;
                case BackendType.Clamav:
                    services.AddSingleton<IScanBackend, ClamavScanBackend>();
                    break;
                case BackendType.DrWeb:
                    services.AddSingleton<IScanBackend, DrWebScanBackend>();
                    break;
                case BackendType.Kes:
                    services.AddSingleton<IScanBackend, KesScanBackend>();
                    break;
                case BackendType.Comodo:
                    services.AddSingleton<IScanBackend, ComodoScanBackend>();
                    break;
                case BackendType.Sophos:
                    services.AddSingleton<IScanBackend, SophosScanBackend>();
                    break;
                case BackendType.McAfee:
                    services.AddSingleton<IScanBackend, McAfeeScanBackend>();
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }
}